package com.xjm.i1.core.resolver.impl;

import cn.hutool.core.util.ArrayUtil;
import com.xjm.i1.constant.*;
import com.xjm.i1.core.resolver.AbstractColorContrastGrayResolver;
import com.xjm.i1.core.struct.location.MaxDataPointMap;
import com.xjm.i1.core.struct.location.Point;
import com.xjm.i1.entity.ColorContrastArea;
import com.xjm.i1.entity.ColorGrayArea;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * i1原始数据中灰度偏差与阶调解析器
 **/
public class ColorContrastGrayResolver extends AbstractColorContrastGrayResolver {

    private static AreaEnum areaSign;

    private static List<MaxDataPointMap> pointMaps;

    // 构建数据映射
    static {
        areaSign = AreaEnum.CONTRAST;
        pointMaps = new LinkedList<MaxDataPointMap>();

        DataTypeEnum max = DataTypeEnum.MAX;
        int maxRow = max.getRow();

        String[] colorGrayValOrder = ColorGrayArea.getColorGrayValOrder();
        MaxGrayEnum[] maxGrayEnumOrdered = MaxGrayEnum.values();
        ColorEnum[] maxColorEnumOrdered = ColorEnum.getMaxColorEnumOrdered();
        String[] colorContrastValOrder = ColorContrastArea.getColorContrastValOrder();

        // 阶调颜色Y轴坐标，每个颜色最多获取16个
        int [] contrastPointY = {4, 4, 7, 6, 5};
        int contrastMaxRow = 16;
        int contrastCount = 1;
        int contrastIndex = 0;

        // 灰度所取行数,0开始
        int [] grayPointX = {3, 7, 11, 15};
        int graySignIndex = 0;
        int grayMaxColume = 3;

        // 阶调坐标组
        List<Point> contrastPoints = new ArrayList(contrastMaxRow);

        // 第一层：数据最大行数
        for (int i=0; i<maxRow; i++) {


            // 第二层-1: 获取灰度数据
            if ( ArrayUtil.contains(grayPointX, i) ) {

                List<Point> grayPoints = new ArrayList(grayMaxColume);

                // 第三层: 灰度l a b数据
                for (int y=0; y<3; y++) {
                    grayPoints.add(new Point(i, y+1, colorGrayValOrder[y]));
                }
                pointMaps.add(new MaxDataPointMap(null, maxGrayEnumOrdered[graySignIndex],
                        MaxDataPointMap.PointMapType.COLOR_GRAY, grayPoints));
                graySignIndex ++;
            }

            // 获取阶调
            contrastPoints.add(new Point(i, contrastPointY[contrastIndex], colorContrastValOrder[contrastCount-1]));
            if (contrastCount == contrastMaxRow) {
                pointMaps.add(new MaxDataPointMap(maxColorEnumOrdered[contrastIndex], null,
                        MaxDataPointMap.PointMapType.COLOR_CONTRAS, contrastPoints));
                contrastPoints = new ArrayList(contrastMaxRow);
                contrastIndex++;
                contrastCount=0;
            }
            contrastCount++;
        }

    }

    public ColorContrastGrayResolver() {
        super(areaSign, pointMaps);
    }
}
